{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x160ea533808>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAVIUlEQVR4nO3df5DcdX3H8ecrJKCLphfkoBGl4a6IYzsY4cygVqzgjwkzFajRYgeaWtqzInperVMuvdHr9Ly1Fubm7HTohIhSUAQzUGjHMFimrf8U9AIxBIHiLb+J4fwRBNeByL37x32XLJe93F6yP76f29dj5ma/+9nvZl/zyc3rdj+7+/0qIjAzs/Qsa3cAMzM7NC5wM7NEucDNzBLlAjczS5QL3MwsUctb+WDHHntsrFmzppUPaWaWvO3bt/8kIrrnjre0wNesWcPk5GQrH9LMLHmSHq017iUUM7NEucDNzBLlAjczS5QL3MwsUS5wy725x+vx8XvMZrnALddGRkYYHBx8qbQjgsHBQUZGRtobzCwHXOCWWxHB3r17mZiYeKnEBwcHmZiYYO/evX4mbh2vpZ8DN1sMSYyPjwMwMTHBxMQEAAMDA4yPjyOpnfHM2k6tfBbT19cX/iKPLVZEsGzZ/heLMzMzLm/rKJK2R0Tf3HEvoViuVZZNqlWviZt1sroKXNKgpPsk7ZJ0vaRXSPqapIcl7ch+1jY7rHWW6jXvgYEBZmZmGBgYeNmauFknW3ANXNIJwKeAN0XEryTdCFyQ3fzZiNjazIDWuSTR1dX1sj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wGnZ/sK+OdsHu8F+tr5/32QnJdWzeWdwNvbmHEA+L/s54tk32hvx++kv0pvZpao5JZQzMxslgvczCxRLnAzs0S5wM3MEuUCNzNLlAvczCxRLnAzs0T9P1uR+IeOthYIAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_train = np.array([\n",
    "    [158, 64],\n",
    "    [170, 86],\n",
    "    [183, 84],\n",
    "    [191, 80],\n",
    "    [155, 49],\n",
    "    [163, 59],\n",
    "    [180, 67],\n",
    "    [158, 54],\n",
    "    [170, 67]\n",
    "])\n",
    "Y_train = ['male', 'male', 'male', 'male', 'female', 'female', 'female', 'female', 'female']\n",
    "\n",
    "plt.figure()\n",
    "for i, x in enumerate(X_train):\n",
    "    plt.scatter(x[0], x[1], c='k', marker='x' if Y_train[i] == 'male' else 'D')\n",
    "\n",
    "plt.scatter(155, 70, c='k', marker='o', s=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6.70820393, 21.9317122 , 31.30495168, 37.36308338, 21.        ,\n",
       "       13.60147051, 25.17935662, 16.2788206 , 15.29705854])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[155, 70]])\n",
    "distances = np.sqrt(np.sum((X_train - x) ** 2, axis=1))\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['male', 'female', 'female'], dtype='<U6')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nearest_neighbor_indices = distances.argsort()[:3]\n",
    "nearest_neighbor_genders = np.take(Y_train, nearest_neighbor_indices)\n",
    "nearest_neighbor_genders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'female'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "b = Counter(np.take(Y_train, distances.argsort()[:3]))\n",
    "b.most_common(1)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [1],\n",
       "       [1],\n",
       "       [1],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import LabelBinarizer\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "lb = LabelBinarizer()\n",
    "y_train_binarized = lb.fit_transform(Y_train)\n",
    "y_train_binarized"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['female'], dtype='<U6')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K = 3\n",
    "clf = KNeighborsClassifier(n_neighbors=K)\n",
    "clf.fit(X_train, y_train_binarized.reshape(-1))\n",
    "\n",
    "prediction_binarized = clf.predict(np.array([155, 70]).reshape(1, -1))[0]\n",
    "prediction_label = lb.inverse_transform(prediction_binarized)\n",
    "prediction_label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Binarized labels : [1 1 0 0]\n"
     ]
    }
   ],
   "source": [
    "X_test = np.array([\n",
    "    [168, 65],\n",
    "    [180, 96],\n",
    "    [160, 52],\n",
    "    [169, 67]\n",
    "])\n",
    "y_test = ['male', 'male', 'female', 'female']\n",
    "y_test_binarized = lb.transform(y_test)\n",
    "\n",
    "print('Binarized labels : %s' % y_test_binarized.T[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Binarzied predictions: [0 1 0 0]\n",
      "Binarzied labels: ['female' 'male' 'female' 'female']\n"
     ]
    }
   ],
   "source": [
    "predictions_binarized = clf.predict(X_test)\n",
    "print('Binarzied predictions: %s' % predictions_binarized)\n",
    "print('Binarzied labels: %s' % lb.inverse_transform(predictions_binarized))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.75\n",
      "Precision: 1.0\n",
      "Recall: 0.5\n",
      "F1 Score: 0.6666666666666666\n",
      "MCC: 0.6666666666666666\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "        male       1.00      0.50      0.67         2\n",
      "\n",
      "   micro avg       1.00      0.50      0.67         2\n",
      "   macro avg       1.00      0.50      0.67         2\n",
      "weighted avg       1.00      0.50      0.67         2\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, matthews_corrcoef, classification_report\n",
    "\n",
    "print('Accuracy: %s' % accuracy_score(y_test_binarized, predictions_binarized))\n",
    "print('Precision: %s' % precision_score(y_test_binarized, predictions_binarized))\n",
    "print('Recall: %s' % recall_score(y_test_binarized, predictions_binarized))\n",
    "print('F1 Score: %s' % f1_score(y_test_binarized, predictions_binarized))\n",
    "print('MCC: %s' % f1_score(y_test_binarized, predictions_binarized))\n",
    "print(classification_report(y_test_binarized, predictions_binarized, target_names=['male'], labels=[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predicted wieghts : [70.66666667 79.         59.         70.66666667]\n",
      "Coefficient of determination: 0.6290565226735438 \n",
      "Mean absolute error: 8.333333333333336 \n",
      "Mean squared error: 95.8888888888889 \n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\n",
    "\n",
    "X_train = np.array([\n",
    "    [158, 1],\n",
    "    [170, 1],\n",
    "    [183, 1],\n",
    "    [191, 1],\n",
    "    [155, 0],\n",
    "    [163, 0],\n",
    "    [180, 0],\n",
    "    [158, 0],\n",
    "    [170, 0]\n",
    "])\n",
    "Y_train = [64, 86, 84, 80, 49, 59, 67, 54, 67]\n",
    "\n",
    "X_test = np.array([\n",
    "    [168, 1],\n",
    "    [180, 1],\n",
    "    [160, 0],\n",
    "    [169, 0]\n",
    "])\n",
    "Y_test = [65, 96, 52, 67]\n",
    "\n",
    "K = 3\n",
    "clf = KNeighborsRegressor(n_neighbors=K)\n",
    "clf.fit(X_train, Y_train)\n",
    "predictions = clf.predict(X_test)\n",
    "\n",
    "print('Predicted wieghts : %s' % predictions)\n",
    "print('Coefficient of determination: %s ' % r2_score(Y_test, predictions))\n",
    "print('Mean absolute error: %s ' % mean_absolute_error(Y_test, predictions))\n",
    "print('Mean squared error: %s ' % mean_squared_error(Y_test, predictions))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.41421356]]\n",
      "[[1.73205081]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics.pairwise import euclidean_distances\n",
    "\n",
    "print(\n",
    "    euclidean_distances(\n",
    "        [[1, 1, 1, 1, 1, 1]],\n",
    "        [[1, 1, 0, 1, 1, 0]]\n",
    "    )\n",
    ")\n",
    "\n",
    "print(\n",
    "    euclidean_distances(\n",
    "        [[1, 1, 1, 1, 1, 1]],\n",
    "        [[1, 1, 0, 1, 0, 0]]\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
